package com.Page.exam.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.Page.exam.dto.TestOptionDTO;

/**
 * 
* Copyright: Copyright (c) 2019 YancyChong.All rights reserved.
* 
* @ClassName: TestOptionDAO.java
* @Description: 该类的功能描述
*
* @version: v1.0.0
* @author: YancyChong
* @date: 2019年5月15日 下午8:53:12 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2019年5月15日     User           v1.0.0               修改原因
 */
@Component
public class TestOptionDAO {
    @Autowired
    private JdbcTemplate jdbcTemplate;

//	public TestOptionDTO add(String testId, TestOptionDTO dto) {
//        final String sql = "INSERT INTO  t_question_option (OPTION_CONTENT, `OPTION`, QUESTION_ID) VALUES (?, ?, ?)";
//        KeyHolder keyHolder = new GeneratedKeyHolder();//获取获取数据库生成的自增主键
//
//        jdbcTemplate.update(con -> {
//            PreparedStatement ps = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
//            ps.setString(1, dto.getContent());
//            ps.setString(2, dto.getOption());
//            ps.setString(3, testId);
//            return ps;
//        }, keyHolder);
//        
//        dto.setId(keyHolder.getKey().longValue());//回填主键
//        return dto;
//    }
	
	public int add(String testId, final List<TestOptionDTO> Opdtos) {
        final String sql = "INSERT INTO  options (OPTION_CONTENT, `OPTION`, QUESTION_ID) VALUES (?, ?, ?)";
        //插入选项数据
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
        	@Override
        	public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setString(1, Opdtos.get(i).getContent());
                ps.setString(2, Opdtos.get(i).getOption());
                ps.setString(3, testId);
			}
			@Override
			public int getBatchSize() {
				return Opdtos.size();
			}
        });
        return Opdtos.size();
    }
	
	
	
	/**
	 * 
	* @Function: TestOptionDAO.java
	* @Description: 查询题的选项
	*
	* @param:描述1描述

	* @version: v1.0.0
	* @author: YancyChong
	* @date: 2019年5月17日 下午7:13:37 
	*
	* Modification History:
	* Date         Author          Version            Description
	*---------------------------------------------------------*
	* 2019年5月17日     YancyChong           v1.0.0               修改原因
	 */
	public List<TestOptionDTO> findByTestId(String testId) {
		final String sql = "select id, option_content, `option` from  options where question_id = ?";
		return jdbcTemplate.query(sql, 
                new Object[] {testId},
                new int[] {Types.VARCHAR},
				(ResultSet rs, int rowNum) -> new TestOptionDTO (rs.getInt("id"), rs.getString("option"), rs.getString("option_content"))
				);
	}
	
	
	public int update(String testId, final List<TestOptionDTO> dtos) {
        final String sql = "UPDATE  `options` SET OPTION_CONTENT = ?, `OPTION` = ?, QUESTION_ID = ? WHERE ID = ?";  
        //批量修改
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
        	@Override
        	public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setString(1, dtos.get(i).getContent());
                ps.setString(2, dtos.get(i).getOption());
                ps.setString(3, testId);
                ps.setLong(4, dtos.get(i).getId());
			}
			@Override
			public int getBatchSize() {
				return dtos.size();
			}
        });
        
        return dtos.size();
    }
	
	//删除的方法
	public int delete(String testId) {
        final String sql = "DELETE FROM  `options` WHERE QUESTION_ID = ?";
        return jdbcTemplate.update(sql,  
                new Object[] {testId},
                new int[] {Types.VARCHAR}
                );
    }
}
